using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class PaddleSkill1Lotus : TriggerSkillCollider
{
    private List<GameObject> curLotus;

    public void Init(SkillData data, float radius, Vector3 pos, List<GameObject> list, Action release, int t = 2)
    {
        base.Initialize(data, radius, true);

        moveCount = 2;
        this.release = release;

        this.data = data;
        curLotus = list;
        splitTimes = t;

        transform.position = pos;
        transform.localScale = new Vector3(radius * 0.2f, radius * 0.2f, 1);
        StartCoroutine(ThisSkill(data));
    }


    private SkillData data;
    private int splitTimes;
    public void Split(Vector3 pos)
    {
        if (--splitTimes < 0) return;

        Vector2 AB = transform.position - pos;
        Vector2 BC1 = new(-AB.y, AB.x);
        Vector2 BC2 = new(AB.y, -AB.x);

        GameObject a = FreeList.Instance.GetObject(((owner as Player).curWeapon as PaddleDataSO).lotus1.name);
        GameObject b = FreeList.Instance.GetObject(((owner as Player).curWeapon as PaddleDataSO).lotus1.name);
        a.GetComponent<PaddleSkill1Lotus>()
            .Init(data, radius * 0.8f, transform.position + (Vector3)BC1, curLotus, release, splitTimes);
        b.GetComponent<PaddleSkill1Lotus>()
            .Init(data, radius * 0.8f, transform.position + (Vector3)BC2, curLotus, release, splitTimes);
        curLotus.Add(a);
        curLotus.Add(b);

        if (moveCoroutine != null)
        {

        }
        else
        {

        }

        Destroy();
    }

    private IEnumerator ThisSkill(SkillData info)
    {
        for (int i = 0; i < 10; i++)
        {
            foreach (GameObject obj in targets.ToList())
            {
                if (obj.TryGetComponent(out Damageable tar))
                {
                    owner.ApplyDamage(tar, info, canFeedback);
                }
            }
            yield return new WaitForSeconds(1f);
        }
        Destroy();
    }

    private int moveCount;
    private Coroutine moveCoroutine;
    public void Move(SkillData data)
    {
        if (moveCoroutine != null) StopCoroutine(moveCoroutine);
        moveCoroutine = StartCoroutine(MoveCoroutine(data.MousePos, 5, 1));
        if (--moveCount == 0) StartCD();
    }

    private IEnumerator MoveCoroutine(Vector2 target, float speed, float time)
    {
        float elapsed = 0f;

        while (elapsed < time)
        {
            elapsed += Time.deltaTime;
            float step = speed * Time.deltaTime;
            transform.position = Vector2.MoveTowards(transform.position, target, step);
            yield return null;
        }
    }

    private Action release;
    public override void Destroy()
    {
        curLotus.Remove(gameObject);
        base.Destroy();
        if (curLotus.Count == 0) StartCD();
        if (moveCoroutine != null)
        {
            StopCoroutine(moveCoroutine);
            moveCoroutine = null;
        }
    }

    private void StartCD()
    {
        if (owner is not Player p) return;
        if (!p.CheckSkillCD(1)) return;
        owner.StartSkillCD(1);
        if (p.controller.GetCurSkill() == 1) p.controller.CancelSkill();
        release?.Invoke();
    }
}